package net.linlan.plat.comm.aspect;

import lombok.extern.slf4j.Slf4j;
import net.linlan.utils.entity.BaseEntity;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import java.sql.Timestamp;
import java.util.Date;


/**
 *
 * Filename:ObjSaveAspect.java
 * 对象保存,修改,切面处理类
 * @author LiXinhai
 * createtime 2021年11月23日14:10:22
 * @version 1.0
 * @since 1.0
 *
 */
@Aspect
@Component
@Slf4j
public class ObjSaveAspect {

    @Pointcut(value = "execution(* net.linlan.commons.db.mybatis.MybatisBaseDao.save(..))")
    public void savePointCut() {

    }

    /**
     * 对象保存，自动赋值创建时间、修改时间、创建人
     * @param point 切面point
     * @throws Throwable 异常
     */
    @Before("savePointCut()")
    public void before(JoinPoint point) throws Throwable {
        Object[] args = point.getArgs();

        if(args!=null&&args.length>0){
            Object obj = args[0];
            if(obj instanceof BaseEntity){
                BaseEntity baseEntity = (BaseEntity) obj;
                Date date = new Date();
                if(baseEntity.getCreateTime()==null){
                    baseEntity.setCreateTime(date);
                }
                if(baseEntity.getLastTime()==null){
                    baseEntity.setLastTime(date);
                }
                if(baseEntity.getCreatorId()==null){

                }
            }
        }
    }


    @Pointcut(value = "execution(* net.linlan.commons.db.mybatis.MybatisBaseDao.update(..))")
    public void updatePointCut() {

    }

    /**
     * 对象修改，自动赋值修改时间
     * @param point 切面point
     * @throws Throwable 异常
     */
    @Before("updatePointCut()")
    public void beforeUpdate(JoinPoint point) throws Throwable {
        Object[] args = point.getArgs();
        if(args!=null&&args.length>0){
            Object obj = args[0];
            if(obj instanceof BaseEntity){
                BaseEntity baseEntity = (BaseEntity) obj;
                if(baseEntity.getLastTime()==null){
                    baseEntity.setLastTime(new Timestamp(System.currentTimeMillis()));
                }
            }
        }
    }


}
